﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Threading;

namespace PepsiTwitterFeed
{
    public partial class Form1 : Form
    {
        string m_ConnectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa ; Password=sa";
        public Form1()
        {
            InitializeComponent();
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Bt_stop.Enabled = true;
            Bt_Start.Enabled = false;

            //GetFeed();
            Thread pepsiFeed = new Thread(new ThreadStart(GetFeed));
            pepsiFeed.Start();
        }

        private void GetFeed()
        {
            while (!Bt_Start.Enabled)
            {
                string updatedFeed8 = "[Font Val=\"Century Gothic\"]";
                string updatedFeed9 = "[Font Val=\"Century Gothic\"]";

                try
                {
                    XmlDocument xdoc = new XmlDocument();

                    xdoc.Load("http://a.wtx.me/customtour/lincolnhangout12/rss.aspx?numchars=2400");

                    XmlNodeList nList = xdoc.SelectNodes("/MSGS/MSG");

                    int length = nList.Count / 2;


                    string miniSpace = " ";
                    string LargeSpace = "      ";
                    for(int i =0 ; i < length; i++)
                    {
                        XmlNode msg = nList[i];
                        string msgText = "[Color Val=\"#FFFFFF\"]" + miniSpace + msg.ChildNodes[0].InnerText;
                        string author = "[Color Val=\"#BABABA\"]" + miniSpace + msg.ChildNodes[1].InnerText;
                        string vendor = msg.ChildNodes[2].InnerText;
                        
                        if( vendor == "google+")
                            updatedFeed8 += LargeSpace + "[IMAGETAG ID=\"705\"]" + author + msgText;
                        else if(vendor =="youtube")
                            updatedFeed8 += LargeSpace + "[IMAGETAG ID=\"707\"]" + author + msgText;
                        else if(vendor =="twitter")
                            updatedFeed8 += LargeSpace + "[IMAGETAG ID=\"706\"]" + author + msgText;
                    }

                    for (int i = length; i < nList.Count; i++)
                    {
                        XmlNode msg = nList[i];
                        string msgText = "[Color Val=\"#FFFFFF\"]" + miniSpace + msg.ChildNodes[0].InnerText;
                        string author = "[Color Val=\"#BABABA\"]" + miniSpace + msg.ChildNodes[1].InnerText;
                        string vendor = msg.ChildNodes[2].InnerText;

                        if (vendor == "google+")
                            updatedFeed9 += LargeSpace + "[IMAGETAG ID=\"705\"]" + author + msgText;
                        else if (vendor == "youtube")
                            updatedFeed9 += LargeSpace + "[IMAGETAG ID=\"707\"]" + author + msgText;
                        else if (vendor == "twitter")
                            updatedFeed9 += LargeSpace + "[IMAGETAG ID=\"706\"]" + author + msgText;
                    }
                }
                catch (Exception e)
                {
                    string error = e.Message;
                    updatedFeed8 = "[Font Val=\"Century Gothic\"]   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square";
                    updatedFeed9 = "[Font Val=\"Century Gothic\"]   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square   Welcome to Times Square";
                }
                finally
                {
                   // updatedFeed = "[Color Val=\"#FFFFFF\"]    [IMAGETAG ID=\"688\"][Font Val=\"FuturaStd-Bold\"]    @pepsi[Font Val="Futura Md"]    One more reason to love today: Our #NickiMinajConcert livestream on http://pepsi.com/ is on at 9:30pm ET! #dontforget    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @JustiziAa[Font Val="Futura Md"]    #NickiMinajConcert id like to be there    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @CBwifey_11[Font Val="Futura Md"]    Going 2 a #NickiMinajConcert would b AMAZING!!!!! :-)    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @MellzMystro[Font Val="Futura Md"]    I'm sorry I can't miss the #NickiMinajConcert live stream !!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Jauan_Levis514[Font Val="Futura Md"]    #NickiMinajConcert is on my bucket list    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @xoxoEnomis_[Font Val="Futura Md"]    #NickiMinajConcert , I Mustt GO :)    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @ItsChelsBitchhh[Font Val="Futura Md"]    I wanna go to a #NickiMinajConcert    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @SCAFFBEEZYjr[Font Val="Futura Md"]    @NICKIMINAJ  i just cant wait till Tuesday #NickiMinajConcert livestream!!!!!!!!!!!!! :P    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @charjacobs1[Font Val="Futura Md"]    I wanna go to the #NickiMinajConcert    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Meeyah143[Font Val="Futura Md"]    If i went to #NickiMinajConcert I would pass out. I love Nicki Minaj she is so dope.    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @furgee221[Font Val="Futura Md"]    hot day   ice cold pepsi = best #summer ever  #LiveForNow    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @sllewyma[Font Val="Futura Md"]    cant wait to view the #NickiMinajConcert !!!!!!!!!!!!!!!!!!!!!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @Retroo_LOVEE[Font Val="Futura Md"]    #NickiMinajConcert  i wanna go!    [IMAGETAG ID="688"][Font Val="FuturaStd-Bold"]    @katiediseroad[Font Val="Futura Md"]    #NickiMinajConcert is trending OH MY GOSH i wanna goooo!";
                }


                UpdateToDB(updatedFeed8, "[Lincoln8]");
                UpdateToDB(updatedFeed9, "[Lincoln9]");
                Thread.Sleep(180000);
            }
        }

        private void UpdateToDB(string dataTagInfo, string TageName)
        {
            SqlConnection conn = new SqlConnection(m_ConnectionString);
            try
            {
   

                conn.Open();

                string sqlcmd = "update DataTag set LastValue = '" + dataTagInfo.Replace("'", "''") + "' where Name = '" + TageName + "'";

                SqlCommand cmd = new SqlCommand(sqlcmd, conn);
                cmd.ExecuteNonQuery();


            }
            catch (Exception e)
            {
            }
            conn.Close();
        }

        private void Bt_stop_Click(object sender, EventArgs e)
        {
            Bt_Start.Enabled = true;
 
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Bt_Start.Enabled = true;
        }


    }
}
